/**
 * 仓库 - 单个物品
 */
class DepotItem extends ItemRendererBase {


	public constructor() {
		super();
		this.skinName = DepotItemSkin;

		this.btnSell.addEventListener(egret.TouchEvent.TOUCH_TAP, this.sellTap, this);
		this.btnSkip.addEventListener(egret.TouchEvent.TOUCH_TAP, this.skipTap, this);
	}

	// 名称
	private labTitle: eui.Label;
	// 图标
	private imgPlant: eui.Image;
	// 拥有
	private labHave: eui.Label;
	// 重量
	private labKg: eui.Label;
	// 价值
	private labPrice: eui.Label;
	// 出售
	private btnSell: eui.Button;
	// 跳转
	private btnSkip: eui.Button;
	// 出售数量
	private elabSell: eui.EditableText;



	protected dataChanged() {
		this.labTitle.text = this.data.name;
		if (Number(this.data.type) === 1) {
			this.imgPlant.scaleX = 0.5;
			this.imgPlant.scaleY = 0.5;
			this.imgPlant.source = "plant_" + this.data.plant_id + "_png";
		} else {
			this.imgPlant.source = "fruit_" + this.data.plant_id + "_png";
		}
		if (this.data.type == 2) {
			this.labKg.text = String(Math.round(this.data.weight * this.data.num)) + "g";
		}
		this.labHave.text = String(this.data.num);
		this.labPrice.text = this.data.price;
	}



	/**
	 * 出售
	 */
	private sellTap() {
		if (this.data.plant_id == 1 || this.data.plant_id == 2) {
			// 直接出售
			var pid = this.data.plant_id;
			var jin_num = Number(this.elabSell.text);
			Net.ins.send(['sale'], { "plant_id": this.data.plant_id, "jin_num": jin_num }, (e) => {
				PlayerSelfData.gold = Number(PlayerSelfData.gold) + Number(e['gold']);
				EventManager.instance.dispatch(EventName.USER_GOLD_CHANGE, PlayerSelfData.gold);
				this.data.num = this.data.num - e['num'];
				if (this.data.type == 2) {
					this.labKg.text = String(Math.round(this.data.weight * this.data.num)) + "g";
				}
				this.labHave.text = String(this.data.num);
				SceneManager.ins.alert("成功出售500g\n金币+" + e['gold'])
				// 这里没做客户端储存
			});
		} else {
			// 跳转
			if (this.data.type == 2) {
				platform.skipExchange(3);
			} else {
				platform.skipExchange(1);
			}
		}
	}



	/**
	 * 兑换
	 */
	private skipTap() {
		if (this.data.plant_id == 1 || this.data.plant_id == 2) {
			// 不能兑换
			SceneManager.ins.alert("不能兑换");
		} else {
			// 跳转
			if (this.data.type == 2) {
				platform.skipExchange(2);
			} else {
				platform.skipExchange(1);
			}
		}
	}

}